package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.GameUtil;

import java.awt.*;

/**
 * @author 刘昱含
 * @date 2021/7/22 14:07
 */
public class Planet extends AbstractSolarObject {

    private int longAxis;
    private int shortAxis;

    private double theta;
    private String name;

    public Planet(){}
    public Planet(String name,String imgName,double AU,int T,double e){
        this.name = name;
        this.longAxis = getLongAxisBy(AU);
        this.shortAxis = getShortAxisByLongAxisAndE(e);
        this.img = GameUtil.getImage(imgName);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.speed = getSpeedByT(T);
        this.x = Constant.WINDOW_WIDTH / 2 + this.longAxis;
        this.y = Constant.WINDOW_HEIGHT / 2 + this.shortAxis;;

    }
    private double getSpeedByT(int T) {
        return 365.0 / T * 0.1;
    }

    @Override
    public void draw(Graphics g) {
        g.drawImage(img,x,y,null);
        drawName(g);
        move();
    }

    private void drawName(Graphics g){
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString(this.name,x,y - 15);
        g.setColor(c);
    }

    @Override
    public void move() {
        x = (int)(longAxis * Math.cos(theta) + Constant.WINDOW_WIDTH / 2);
        y = (int)(shortAxis * Math.sin(theta) + Constant.WINDOW_HEIGHT / 2);
        theta += speed;
    }
    private int getLongAxisBy(double AU){
        return (int) (AU * 100 / 2);
    }
    private int getShortAxisByLongAxisAndE(double e){
        return (int) (this.longAxis * Math.sqrt(1 - Math.pow(e,2)));
    }

    }

